// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial

import { StyleMetrics, ScrollView } from "std-widgets-impl.slint";

export component LineEditInner inherits Rectangle {
    callback accepted(string);
    callback edited(string);
    in-out property <string> placeholder-text;
    in-out property font-size <=> input.font-size;
    in-out property text <=> input.text;
    in-out property placeholder-color <=> placeholder.color;
    in-out property enabled <=> input.enabled;
    in-out property <bool> has-focus: input.has-focus;
    in-out property input-type <=> input.input-type;
    in-out property horizontal-alignment <=> input.horizontal-alignment;
    in-out property read-only <=> input.read-only;


    public function select-all() {
        input.select-all();
    }
    public function cut() {
        input.cut();
    }
    public function copy() {
        input.copy();
    }
    public function paste() {
        input.paste();
    }    

    min-height: input.preferred-height;
    min-width: max(50px, placeholder.min-width);
    clip: true;
    forward-focus: input;
    placeholder := Text {
        height: 100%; width: 100%;
        vertical-alignment: center;
        text: (root.text == "" && input.preedit-text == "") ? root.placeholder-text : "";
        font-size: input.font-size;
        font-italic: input.font-italic;
        font-weight: input.font-weight;
        font-family: input.font-family;
    }
    input := TextInput {
        property <length> computed_x;
        x: min(0px, max(parent.width - self.width, self.computed_x));
        width: max(parent.width, self.preferred-width);
        height: 100%;
        color: self.enabled ? StyleMetrics.textedit-text-color : StyleMetrics.textedit-text-color-disabled;
        cursor-position-changed(cpos) => {
            if (cpos.x + self.computed_x < StyleMetrics.layout-padding) {
                self.computed_x = - cpos.x + StyleMetrics.layout-padding;
            } else if (cpos.x + self.computed_x > parent.width - StyleMetrics.layout-padding) {
                self.computed_x = parent.width - cpos.x - StyleMetrics.layout-padding;
            }
        }
        accepted => { root.accepted(self.text); }
        edited => { root.edited(self.text); }
        vertical-alignment: center;
        single-line: true;
    }
}

export component TextEdit inherits ScrollView {
    in property <length> font-size <=> input.font-size;
    in-out property <string> text <=> input.text;
    has-focus: input.has-focus;
    enabled <=> input.enabled;
    in property <TextWrap> wrap <=> input.wrap;
    in property horizontal-alignment <=> input.horizontal-alignment;
    in property read-only <=> input.read-only;
    callback edited(string);
    forward-focus: input;

    public function select-all() {
        input.select-all();
    }
    public function cut() {
        input.cut();
    }
    public function copy() {
        input.copy();
    }
    public function paste() {
        input.paste();
    }

    horizontal-stretch: 1;
    vertical-stretch: 1;

    viewport-width: root.wrap == TextWrap.word-wrap ? root.visible-width : max(root.visible-width, input.preferred-width);
    viewport-height: max(self.visible-height, input.preferred-height);

    Rectangle {
        background: root.enabled ? StyleMetrics.textedit-background : StyleMetrics.textedit-background-disabled;
    }

    input := TextInput {
        enabled: true;
        edited => { root.edited(self.text); }
        color: self.enabled ? StyleMetrics.textedit-text-color : StyleMetrics.textedit-text-color-disabled;
        single-line: false;
        wrap: word-wrap;
        cursor-position-changed(cpos) => {
            if (cpos.x + root.viewport-x < StyleMetrics.layout-padding) {
                root.viewport-x = min(0px, max(parent.visible-width - self.width,  - cpos.x + StyleMetrics.layout-padding ));
            } else if (cpos.x + root.viewport-x > parent.visible-width - StyleMetrics.layout-padding) {
                root.viewport-x = min(0px, max(parent.visible-width - self.width,  parent.visible-width - cpos.x - StyleMetrics.layout-padding ));
            }
            if (cpos.y + root.viewport-y < StyleMetrics.layout-padding) {
                root.viewport-y = min(0px, max(parent.visible-height - self.height,  - cpos.y + StyleMetrics.layout-padding ));
            } else if (cpos.y + root.viewport-y > parent.visible-height - StyleMetrics.layout-padding - 20px) {
                // FIXME: font-height hardcoded to 20px
                root.viewport-y = min(0px, max(parent.visible-height - self.height,  parent.visible-height - cpos.y - StyleMetrics.layout-padding - 20px));
            }
        }
    }
}
export component AboutSlint {
    preferred-height: 100%;
    preferred-width: 100%;
    min-height: lay.min-height;
    min-width: lay.min-width;
    lay := VerticalLayout {
        padding: 12px;
        spacing: 8px;
        alignment: start;
        t := Text {
            text: "Made with";
            font-size: 24px;
            font-weight: 700;
            horizontal-alignment: center;
        }
        Image {
            source: StyleMetrics.dark-color-scheme ? @image-url("slint-logo-dark.svg") : @image-url("slint-logo-light.svg");
            preferred-width: 256px;
            min-height: 48px;
        }
        Text {
            text: "Version 1.0.3\nhttps://slint-ui.com/";
            font-size: 10px;
            horizontal-alignment: center;
        }
    }
}
